class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    OnPageLoad() {
        // 回车直接进行搜索
        $("panel_search").bind('keyup', function (e) {
            let key = e.keyCode;
            if (key === 13) {
                _Instance.Fetch();
            }
        });
        $('#loading').hide();
    }
    SetCriterion(item) {
        item['Begin'] = $("#Begin").val();
        item['End'] = $("#End").val();
        item['SupplierNameFilterValue'] = $("#SupplierNameFilterValue").val();
        item['MaterialCode'] = $("#MaterialCodeleFilterValue").val();
        item['MaterialName'] = $("#MaterialNameFilterValue").val();
        return item;
    }
    GetFilter() {
        let item = super.GetFilter(0);
        // 设置条件
        item = this.SetCriterion(item);
        return JSON.stringify(item);
    }
    EmptyTableContent() {
        $("#tab_data01 tbody").empty();
    }
    SetTableContent(result) {
        $("#tab_data01 tbody").empty();

        let content_01 = '';
        this.rows = result.length;
        for (let i = 0; i < result.length; i++) {
            if (parseInt(result[i].rowFlag) === 0) {
                content_01 += '<tr>';
                content_01 += '<td>' + formatNullData(result[i].supplierName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].styleNo) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialCode) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialColourName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialColourNumber) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].specification) + '</td>';
                content_01 += '<td style="text-align:right; mso-number-format:0.00">' + formatNullData(result[i].mrpQuantity) + '</td>';
                content_01 += '<td style="text-align:right; mso-number-format:0.00">' + formatNullData(result[i].poQuantity) + '</td>';
                content_01 += '<td style="text-align:right; mso-number-format:0.00">' + formatNullData(result[i].amount) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].signedDate) + '</td>';
                content_01 += '</tr>';
            } 
            if (parseInt(result[i].rowFlag) === 1) {
                content_01 += '<tr style="background-color:#ffe4b5; color:#000;">';
                content_01 += '<td colspan="9" style="font-weight:bold; text-align:right; padding-right:20px;">（小计）</td>';
                content_01 += '<td style="text-align:right; mso-number-format:0.00;">' + formatNullData(result[i].amount) + '</td>';
                content_01 += '<td></td>';
                content_01 += '</tr>';
                //_totalQuantity += result[i].quantityOfContract === null ? 0 : result[i].quantityOfContract;
                //_totalAmount += result[i].amount === null ? 0 : result[i].amount;
            }
            if (parseInt(result[i].rowFlag) === 2) {
                content_01 += '<tr style="background-color:#00f9fc; color:#000;">';
                content_01 += '<td colspan="9" style="font-weight:bold; text-align:right; padding-right:20px;">（总计）</td>';
                content_01 += '<td style="text-align:right; mso-number-format:0.00;">' + formatNullData(result[i].amount) + '</td>';
                content_01 += '<td></td>';
                content_01 += '</tr>';
            }
            $("#tab_data01 tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tab_data01");

        this.AutoAdjustTableHeightForLines();   // 自适应高度设置
        $(window).resize();                     // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    //《按钮》搜索
    DoSearch() {
        if (formatNullInputValue($.trim($("#Begin").val())) === null || formatNullInputValue($.trim($("#End").val())) === null) {
            ShowErrorMessagePanel('检索条件日期必须填写。');
            return false;
        } else {
            this.FetchForPage(1);
        }
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _rc = Math.floor((h - 147 - 25) / 25);
        let _dh = _rc * 25;
        let _tah = this.rows * 25;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#div_content").css({"max-height": _dh, height: _dh});
    }
    DoPrint() {
        // 得到要打印的内容
        let _print_content =
                '<table style="width:100%; margin:0 auto; text-align:left;">' +
                '    <thead>' +
                '        <tr><td colspan="10" style="font-size:16px; font-weight:bold; padding-bottom:10px; border: none;">采购合同统计报表</td></tr>' +
                '        <tr>' +
                '            <th>供应商</th>' +
                '            <th>款号</th>' +
                '            <th>材料名称</th>' +
                '            <th>材料编码</th>' +
                '            <th>材料颜色</th>' +
                '            <th>材料色号</th>' +
                '            <th>材料规格</th>' +
                '            <th>调达数量</th>' +
                '            <th>订货数量</th>' +
                '            <th>采购金额</th>' +
                '            <th>落单日期</th> ' +
                '        </tr>' +
                '    </thead>' +
                '    <tbody>' + $("#tab_data01 tbody").html() +
                '    </tbody>' +
                '</table>';
        // 生成并打印ifrme  
        let _iframe = document.createElement('IFRAME');
        _iframe.setAttribute('style', 'position:absolute; width:0px; height:0px; left:-500px; top:-500px;');
        document.body.appendChild(_iframe);
        let _doc = _iframe.contentWindow.document;
        _doc.write('<!DOCTYPE html>');
        _doc.write('<html><head><meta charset="utf-8">');
        // 引入打印的专有CSS样式
        _doc.write('<style type="text/css" media="print">' +
                '    @page {' +
                '        size: A4 landscape;' +
                //'    margin: 10mm 10mm 10mm 10mm;' +
                '        @bottom-right {font-size:9pt; content: "页 " counter(page) " / " counter(pages);}' +
                '    }' +
                '    table {border-collapse: collapse; border-spacing: 0;}' +
                '    thead {display: table-header-group;}' +
                '    th {padding: 3px 2px; font-size: 12px; font-weight: bold; text-align: center; border: 1px solid #000;}' +
                '    td {padding: 3px 2px; font-size: 12px; font-weight: normal; text-align: center; border: 1px solid #000;}' +
                '</style>');
        _doc.write('</head><body>');
        _doc.write(_print_content);
        _doc.write('</body></html>');
        _doc.close();
        _iframe.contentWindow.print();
        document.body.removeChild(_iframe);
    }
    TableToExcel(table, filename) {
        if (!table.nodeType)
            table = document.getElementById(table);
        let _content =
                '<table border="1">' +
                '    <caption>采购合同统计报表</caption>' +
                '    <thead>' +
                '        <tr>' +
                '            <td>供应商</td>' +
                '            <td>款号</td>' +
                '            <td>材料名称</td>' +
                '            <td>材料编码</td>' +
                '            <td>材料颜色</td>' +
                '            <td>材料色号</td>' +
                '            <td>材料规格</td>' +
                '            <td>调达数量</td>' +
                '            <td>订货数量</td>' +
                '            <td>采购金额</td>' +
                '            <td>落单日期</td> ' +
                '        </tr>' +
                '    </thead>' +
                '    <tbody>' + table.getElementsByTagName("tbody")[0].innerHTML +
                '    </tbody></table>';
        // 包装成一个完整的HTML文档，设置charset为urf-8以防止中文乱码
        let html = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">' +
                '<head><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=utf-8" />' +
                '<style type="text/css">' +
                '    td {mso-number-format:"\@";}' +
                '    caption {' +
                '        height:30pt; font-size:14.0pt; font-weight:700; text-align:center;' +
                '    }' +
                '</style>' +
                '</head><body>' + _content + '</body></html>';

        // 实例化一个Blob对象，其构造函数的第一个参数是包含文件内容的数组，第二个参数是包含文件类型属性的对象
        let blob = new Blob([html], {type: "application/vnd.ms-excel"});
        window.URL = window.URL || window.webkitURL;
        let link = window.URL.createObjectURL(blob);
        let a = document.createElement("a");
        a.download = filename;
        a.href = link;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
}

var _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#search_btn").hide(); // 无需搜索，隐藏搜索按钮
    $("#MaterialCodeleFilterValue").bigAutocomplete({
        url: parent.basePath + "/master/material/fetchMaterialCodeForAutoComplate.html"
    });

    _Instance = new MyPage(0, null, null, null, null, null, null, null,
            "/purchase/report/po_statistics/fetchAll.html",
            "");

    window.laydate.render({
        elem: '#Begin',
        theme: '#393D49',
        trigger: 'click'
    });
    window.laydate.render({
        elem: '#End',
        theme: '#393D49',
        trigger: 'click'
    });

    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    if (_Instance !== null)
        _Instance.AutoAdjustTableHeightForLines();
}
